package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.common.PageInfo;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //新增分类
    @PostMapping
    public CommonResult add(@RequestBody Category category){
        categoryService.addCategory(category);
        return CommonResult.success("添加分类成功");
    }

    //分页查询
    @GetMapping("/page")
    public CommonResult page(
                             //默认page为1
                             @RequestParam(defaultValue = "1") Integer page ,
                             //默认pageSize为10
                             @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Category> page1 = categoryService.page(page, pageSize);
        return CommonResult.success(page1);
    }



    //删除分类
    @DeleteMapping
    public CommonResult deleteCategory(String id){
        boolean res = categoryService.deleteCategory(id);
        if (!res){
            return CommonResult.error("删除分类失败");
        }
        return CommonResult.success("删除分类成功");
    }

    //修改分类
    @PutMapping
    public CommonResult updateCategory(@RequestBody Category category){
        boolean res = categoryService.updateCategory(category);
        if (!res){
            return CommonResult.error("修改分类失败");
        }
        return CommonResult.success("修改分类成功");
    }

    //分类下拉列表
    @GetMapping("/list")
    public CommonResult list(Integer type){
        List<Category> list = categoryService.getList(type);
        return CommonResult.success(list);
    }



}
